package com.kamistoat.meimeichat.meimeichatclient.socket.handlers;

import com.kamistoat.meimeichat.meimeichatclient.application.UIServiceConnector;
import com.kamistoat.meimeichat.meimeichatclient.socket.NettyClientBizHandler;
import com.kamistoat.meimeichat.meimeichatui.view.chat.IChatMethod;
import com.kamistoat.meimeichatserveraggrement.protocol.friend.SearchFriendResponse;
import com.kamistoat.meimeichatserveraggrement.protocol.friend.dto.UserDto;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import javafx.application.Platform;

import java.util.List;

/**
 * 客户端处理服务器返回的 好友查询消息响应
 */
public class SearchFriendHandler extends NettyClientBizHandler<SearchFriendResponse> {

    public SearchFriendHandler(UIServiceConnector uiServiceConnector) {
        super(uiServiceConnector);
    }

    /**
     * @param channel
     * @param msg
     * @Warning:子类只需要重写该方法
     */
    @Override
    public void channelRead(Channel channel, SearchFriendResponse msg) {
        List<UserDto> list = msg.getList();
        if (null == list || list.isEmpty()) return;
        IChatMethod chat = uiServiceConnector.getIChatMethod();
        Platform.runLater(() -> {
            for (UserDto user : list) {
                chat.addLuckFriend(user.getUserId(), user.getUserNickName(), user.getUserHead(), user.getStatus());
            }
        });
    }

}
